<?php

/**
 * 投票模块-项目管理
 * @desc
 */
class Api_Vote_item extends PhalApi_Api {

    public function getRules() {
        return [
            '*'=>[
                'token'        => ['name' => 'token', 'type' => 'string', 'min' => 12, 'max' => 12, 'require' => true, 'desc' => 'tokenID'],
            ],
            'add'    => [
                'item'         => ['name' => "item", 'type' => 'string', 'require' => true, 'desc' => '项目名称'],
                'userLimit'    => ['name' => "userLimit", 'type' => 'int', 'default' => 1, 'min' => 1, 'require' => true, 'desc' => '用户投票数限制数量/每天'],
                'nomineeLimit' => ['name' => "nomineeLimit", 'type' => 'int', 'default' => 1, 'min' => 1, 'require' => true, 'desc' => '候选人投票数限制数量/用户/每天'],
                'startTime'    => ['name' => "startTime", 'type' => 'date', 'require' => true, 'desc' => '投票活动开始时间'],
                'endTime'      => ['name' => "endTime", 'type' => 'date', 'require' => true, 'desc' => '投票活动结束时间'],
            ],

            'update'    => [
                'id'         => ['name' => "id", 'type' => 'int', 'require' => true, 'desc' => 'id'],
                'item'         => ['name' => "item", 'type' => 'string', 'require' => true, 'desc' => '项目名称'],
                'userLimit'    => ['name' => "userLimit", 'type' => 'int', 'default' => 1, 'min' => 1, 'require' => true, 'desc' => '用户投票数限制数量/每天'],
                'nomineeLimit' => ['name' => "nomineeLimit", 'type' => 'int', 'default' => 1, 'min' => 1, 'require' => true, 'desc' => '候选人投票数限制数量/用户/每天'],
                'startTime'    => ['name' => "startTime", 'type' => 'date', 'require' => true, 'desc' => '投票活动开始时间'],
                'endTime'      => ['name' => "endTime", 'type' => 'date', 'require' => true, 'desc' => '投票活动结束时间'],
            ],

            'info'       => [
                'id'  => ['name' => "id", 'type' => 'int', 'require' => true, 'desc' => 'id'],
            ],
            'delete' => [
                'id'  => ['name' => "id", 'type' => 'int', 'require' => true, 'desc' => 'id'],
            ],
            'lists'  => [
            ]


        ];
    }
    /**
     *投票项目添加
     * @desc item存在将更新，不存在会添加
     * @return int ret HTTP状态码,200成功,其他失败
     * @return int id 项目id
     * @return string item 项目名称
     * @return int userLimit 用户投票数限制数量/每天
     * @return int nomineeLimit 候选人投票数限制数量/用户/每天
     * @return date startTime 投票活动开始时间
     * @return date endTime 投票活动结束时间
     */
    function add() {
        $db = new Domain_Vote();

        $data = $db->itemAdd($this->item, $this->userLimit, $this->nomineeLimit, $this->startTime, $this->endTime);
        if ($data == NULL) {
            throw new PhalApi_Exception("添加失败", 10001);
        }
        return $data;
    }
    /**
     *投票项目更新
     * @desc item存在将更新，不存在会添加
     * @return int ret HTTP状态码,200成功,其他失败
     * @return int id 项目id
     * @return string item 项目名称
     * @return int userLimit 用户投票数限制数量/每天
     * @return int nomineeLimit 候选人投票数限制数量/用户/每天
     * @return date startTime 投票活动开始时间
     * @return date endTime 投票活动结束时间
     */
    function update() {
        $db = new Domain_Vote();

        $data = $db->itemUpdate($this->item, $this->userLimit, $this->nomineeLimit, $this->startTime, $this->endTime,$this->id);
        if ($data == NULL) {
            throw new PhalApi_Exception("更新失败", 10001);
        }
        return $data;
    }

    /**
     *投票项目删除
     * @desc
     * @return int ret HTTP状态码,200成功,其他失败
     * @return boolean data.isdeleted 是否删除成功
     */
    function delete() {
        $db = new Domain_Vote();

        $data = $db->itemDelete($this->id);
        if ($data == NULL) {
            throw new PhalApi_Exception("删除失败", 10003);
        }
        return ['isdeleted' => true];
    }

    /**
     *投票项目信息获取
     * @desc
     * @return int ret HTTP状态码,200成功,其他失败
     * @return int id 项目id
     * @return string item 项目名称
     * @return int userLimit 用户投票数限制数量/每天
     * @return int nomineeLimit 候选人投票数限制数量/用户/每天
     * @return date startTime 投票活动开始时间
     * @return date endTime 投票活动结束时间
     * @return array lists 候选人名称及票数
     */

    function info() {
        $db = new Domain_Vote();
        $itemInfo = $db->itemGet($this->id);
        if (!$itemInfo) {
            throw new PhalApi_Exception("未找到该项目", 10002);
        };
        unset($itemInfo['id']);
        $itemInfo['lists'] = $db->getlistbyid($this->id);
        return $itemInfo;
    }

    /**
     * 获取投票项目列表
     * @desc  todo 增加分页功能
     * @return int ret HTTP状态码,200成功,其他失败
     * @return array items 投票项目数组
     * @return string item 项目名称
     * @return int userLimit 用户投票数限制数量/每天
     * @return int nomineeLimit 候选人投票数限制数量/用户/每天
     * @return date startTime 投票活动开始时间
     * @return date endTime 投票活动结束时间
     * @return string msg 提示信息
     */
    public function lists() {
        $db = new Domain_Vote();
        return [
            "items" => $db->itemList()
        ];
    }


}
